<?php
Yii::import('application.components.U');
class KategoriController extends GxController
{
    public function actionCreate()
    {
        $model = new Kategori;
        if (!Yii::app()->request->isAjaxRequest)
            return;
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v))
                    $v = get_number($v);
                $_POST['Kategori'][$k] = $v;
            }
            $model->attributes = $_POST['Kategori'];
            $msg = t('save.fail','app');
            if ($model->save()) {
                $status = true;
                $msg = t('save.success.id','app',array('{id}'=>$model->kategori_id));
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            echo CJSON::encode(array('success' => $status, 'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Kategori');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v))
                    $v = get_number($v);
                $_POST['Kategori'][$k] = $v;
            }
            $msg = $msg = t('save.fail','app');
            $model->attributes = $_POST['Kategori'];
            if ($model->save()) {
                $status = true;
                $msg = t('save.success.id','app',array('{id}'=>$model->kategori_id));
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array('success' => $status, 'msg' => $msg));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->kategori_id));
            }
        }
    }
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $msg = 'Data berhasil dihapus.';
            $status = true;
            try {
                $this->loadModel($id, 'Kategori')->delete();
            } catch (exception $ex) {
                $status = false;
                $msg = $ex;
            }
            echo CJSON::encode(array('success' => $status, 'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(400, Yii::t('app',
                'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {
        if (isset($_POST['limit'])) {
            $limit = $_POST['limit'];
        } else {
            $limit = 20;
        }
        if (isset($_POST['start'])) {
            $start = $_POST['start'];
        } else {
            $start = 0;
        }
        $criteria = new CDbCriteria();
        if ((isset($_POST['mode']) && $_POST['mode'] == 'grid') || (isset($_POST['limit']) &&
                isset($_POST['start']))
        ) {
            $criteria->limit = $limit;
            $criteria->offset = $start;
        }
        $model = Kategori::model()->findAll($criteria);
        $total = Kategori::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}
